﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RoleAttributeManager : MonoBehaviour
{
    public static RoleAttributeManager _instance;

    public Text attackValue;//攻击
    public Text defenseValue;//防御
    public Text lifeValue;//生命
    public Text experienceAddition;//经验加成
    public Text skillCooling;//技能冷却
    public Animator panelAni;
    Text moneyValue;//元宝数

    public MainItem mainItem;

    public GameObject equipmentPanelButton;//为了点击空白处关闭
    public GameObject equipmentPanel;//单种装备展示
    public GameObject[] parentObject;//6个装备按钮
   
    public equipmentShow equipmentShowPrefab;

    int nowShow = 0;//此时点击弹框是哪个装备弹出来的

    public GameObject yindaoPanel;
    public GameObject yindao8;
    private void Awake()
    {
        _instance = this;
        Equiped();
    }
    void Start()
    {
        if (UnityEngine.Screen.height > 2000)
        {
            equipmentPanel.transform.localScale = new Vector3(0.6f, 0.6f, 0.6f);
        }
        moneyValue = GameObject.Find("moneyValue").GetComponent<Text>();
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
        yindaoPanel.SetActive(false);
        yindao8.transform.GetChild(0).gameObject.SetActive(false);
        yindao8.transform.GetChild(1).gameObject.SetActive(false);
        if (VariableDocument.GetInstance.yindaoid == 5)
        {
            yindaoPanel.SetActive(true);
            yindaoPanel.transform.GetChild(0).gameObject.SetActive(true);
        }
    }

    void Update()
    {
        attackValue.text = VariableDocument.GetInstance.role_attribute[0].ToString();
        defenseValue.text = VariableDocument.GetInstance.role_attribute[1].ToString();
        lifeValue.text = VariableDocument.GetInstance.role_attribute[2].ToString();
        experienceAddition.text = VariableDocument.GetInstance.role_attribute[3] + "%";
        skillCooling.text = VariableDocument.GetInstance.role_attribute[4] + "%";
    }

    //一键装备
    public void EquipAll()
    {
        if (VariableDocument.GetInstance.yindaoid == 5)
        {
            yindaoPanel.SetActive(false);
            yindaoPanel.transform.GetChild(1).gameObject.SetActive(false);
            yindao8.transform.GetChild(0).gameObject.SetActive(true);
            yindao8.transform.GetChild(1).gameObject.SetActive(true);
            VariableDocument.GetInstance.yindaoid = 9;
        }
        //更改装备与否的图片
        //遍历总库，寻找等级最高的装备
        for (int a = 0; a < mainItem.itemList.Count; a++)
        {
            if (!mainItem.itemList[a].selled)
            {
                //装备
                int index = mainItem.itemList[a].itemCategory;//装备的类型
                                                              //equiped[index]存储是在mainItem的索引
                if (VariableDocument.GetInstance.equiped[index] != -1)//证明已经装备上
                {

                    //此时要判断当前装备等级大小
                    if (mainItem.itemList[VariableDocument.GetInstance.equiped[index]].itemGrade < mainItem.itemList[a].itemGrade)
                    {
                        parentObject[index].GetComponent<Image>().sprite = mainItem.itemList[a].itemImage;
                        VariableDocument.GetInstance.equiped[index] = mainItem.itemList[a].id;
                    }
                }
                else
                {
                    parentObject[index].GetComponent<Image>().sprite = mainItem.itemList[a].itemImage;
                    VariableDocument.GetInstance.equiped[index] = mainItem.itemList[a].id;
                }
            }
            

        }
        //装备前更新角色属性值为未装备时的值
        for (int e = 0; e < 5; e++)
        {
            VariableDocument.GetInstance.role_attribute[e] = VariableDocument.GetInstance.per_role_attribute[e];
        }
        //更改人物的属性
        for (int b = 0; b < 6; b++)
        {
            if (VariableDocument.GetInstance.equiped[b] != -1)
            {
                for (int c = 0; c < 5; c++)
                {
                    
                    VariableDocument.GetInstance.role_attribute[c] += mainItem.itemList[VariableDocument.GetInstance.equiped[b]].attribute[c];
                }
                parentObject[b].transform.GetChild(0).gameObject.SetActive(true);
            }
        }
    }

    //点击属性按钮
    public void ClickAttributeButton()
    {
        if (VariableDocument.GetInstance.yindaoid == 5)
        {
            yindaoPanel.transform.GetChild(0).gameObject.SetActive(false);
            yindaoPanel.transform.GetChild(1).gameObject.SetActive(true);
        }
        panelAni.SetBool("back", false);
        panelAni.SetBool("go", true);
        GameObject.Find("attributeButton").SetActive(false);
    }

    //点击各个装备按钮
    public void ClickEquipBtn(int d)
    {
        nowShow = d;
        //显示弹框
        equipmentPanelButton.SetActive(true);
        equipmentPanel.SetActive(true);
        //调整弹框的位置
        switch (d)
        {
            case 0:
                break;
            default:
                equipmentPanel.transform.localPosition = new Vector3(equipmentPanel.transform.localPosition.x, equipmentPanel.transform.localPosition.y + 200, equipmentPanel.transform.localPosition.z);
                break;
        }
        //销毁弹框panel下的子物体
        for (int i = 0; i < equipmentPanel.transform.childCount; i++)
        {
            Destroy(equipmentPanel.transform.GetChild(i).gameObject);
        }
        //如果已经穿戴上，需要第一个展示卸下
        if(VariableDocument.GetInstance.equiped[d] != -1)//不等于-1说明此时已经穿戴上,并且穿上的装备的id是equiped[d]
        {
            //加载预制体
            equipmentShow newEquipmentShow = Instantiate(equipmentShowPrefab);
            //指定父物体
            newEquipmentShow.transform.SetParent(equipmentPanel.transform);
            //指定大小
            newEquipmentShow.transform.localScale = new Vector3(1f, 1f, 1f);
            newEquipmentShow.transform.localPosition = new Vector3(newEquipmentShow.transform.localPosition.x, newEquipmentShow.transform.localPosition.y, 0);
            //展示的装备的图片
            newEquipmentShow.showImage.sprite = mainItem.itemList[VariableDocument.GetInstance.equiped[d]].itemImage;
            //按钮的图片
            newEquipmentShow.btnImage.sprite = Resources.Load<Sprite>("RoleAttribute/卸下");
            //功能判断，是卸下还是装备
            newEquipmentShow.canLoad = true;
            //传递此时的弹框是哪个装备
            newEquipmentShow.index = d;
            //展示的装备的id号
            newEquipmentShow.id = mainItem.itemList[d].id;
            //装备的名称
            newEquipmentShow.showName.text = mainItem.itemList[VariableDocument.GetInstance.equiped[d]].itemName;
            //装备的等级
            newEquipmentShow.showNum.text = "+" + mainItem.itemList[d].itemGrade;
            //装备的属性加成
            newEquipmentShow.add.text = VariableDocument.GetInstance.equipmentAddText[d, 0] + mainItem.itemList[VariableDocument.GetInstance.equiped[d]].attribute[VariableDocument.GetInstance.equipmentAddIndex[d, 0]] + "\n" + VariableDocument.GetInstance.equipmentAddText[d, 1] + mainItem.itemList[VariableDocument.GetInstance.equiped[d]].attribute[VariableDocument.GetInstance.equipmentAddIndex[d, 1]];
            
        }
        //对mainItem进行遍历，
        for(int f = 0; f < mainItem.itemList.Count; f++)
        {
            if (!mainItem.itemList[f].selled)
            {
                //所有类别属于d的装备皆展示出来
                if (mainItem.itemList[f].itemCategory == d && mainItem.itemList[f].id != VariableDocument.GetInstance.equiped[d])
                {
                    //加载预制体
                    equipmentShow newEquipmentShow = Instantiate(equipmentShowPrefab);
                    //指定父物体
                    newEquipmentShow.transform.SetParent(equipmentPanel.transform);
                    //指定大小
                    newEquipmentShow.transform.localScale = new Vector3(1f, 1f, 1f);
                    newEquipmentShow.transform.localPosition = new Vector3(newEquipmentShow.transform.localPosition.x, newEquipmentShow.transform.localPosition.y, 0);
                    //展示的装备的图片
                    newEquipmentShow.showImage.sprite = mainItem.itemList[f].itemImage;
                    //功能判断，是卸下还是装备
                    newEquipmentShow.canLoad = false;
                    //传递此时的弹框是哪个装备
                    newEquipmentShow.index = d;
                    //展示的装备的id号
                    newEquipmentShow.id = mainItem.itemList[d].id;
                    //装备的名称
                    newEquipmentShow.showName.text = mainItem.itemList[f].itemName;
                    //装备的等级
                    newEquipmentShow.showNum.text = "+" + mainItem.itemList[d].itemGrade;
                    //装备的属性加成
                    newEquipmentShow.add.text = VariableDocument.GetInstance.equipmentAddText[d, 0] + mainItem.itemList[f].attribute[VariableDocument.GetInstance.equipmentAddIndex[d, 0]] + "\n" + VariableDocument.GetInstance.equipmentAddText[d, 1] + mainItem.itemList[f].attribute[VariableDocument.GetInstance.equipmentAddIndex[d, 1]];
                }
            }
        }
    }
    //点击空白处关闭
    public void ClickBlank()
    {
        switch (nowShow)
        {
            case 0:
                break;
            default:
                equipmentPanel.transform.localPosition = new Vector3(equipmentPanel.transform.localPosition.x, equipmentPanel.transform.localPosition.y - 200, equipmentPanel.transform.localPosition.z);
                break;
        }
        equipmentPanel.SetActive(false);
        equipmentPanelButton.SetActive(false);
    }

    //进入此界面调用
    public void Equiped()
    {
        //更改装备与否的图片
        for (int ah = 0; ah < 6; ah++)
        {
            if (VariableDocument.GetInstance.equiped[ah] != -1)//证明已经装备上,equiped[index]存储的是数据总库List的顺序
            {
                parentObject[ah].GetComponent<Image>().sprite = mainItem.itemList[VariableDocument.GetInstance.equiped[ah]].itemImage;
                VariableDocument.GetInstance.equiped[ah] = mainItem.itemList[VariableDocument.GetInstance.equiped[ah]].id;
            }
        }
        //装备前更新角色属性值为未装备时的值
        for (int eh = 0; eh < 5; eh++)
        {
            VariableDocument.GetInstance.role_attribute[eh] = VariableDocument.GetInstance.per_role_attribute[eh];
        }
        //更改人物的属性
        for (int bh = 0; bh < 6; bh++)
        {
            if (VariableDocument.GetInstance.equiped[bh] != -1)
            {
                for (int c = 0; c < 5; c++)
                {
                    VariableDocument.GetInstance.role_attribute[c] += mainItem.itemList[VariableDocument.GetInstance.equiped[bh]].attribute[c];
                }
                parentObject[bh].transform.GetChild(0).gameObject.SetActive(true);
            }
        }
    }
}
